package com.example.music;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.media.MediaPlayer;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.music.entity.DataMusic;
import com.hacknife.carouselbanner.Banner;
import com.hacknife.carouselbanner.interfaces.CarouselImageFactory;

import org.litepal.LitePal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class App extends Application {

    private MediaPlayer mPlayer = new MediaPlayer();
    private List<DataMusic> dataMusics = new ArrayList<>();
    private int i = -1;
    private List<MusicEvent> musicEvents = new ArrayList<>();



    @Override
    public void onCreate() {
        super.onCreate();
        LitePal.initialize(this);
        Banner.init(new ImageFactory());
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                try {
                    mPlayer.reset();
                    i++;
                    if(dataMusics.size()>=i){
                        i=0;
                    }
                    for (MusicEvent musicEvent: musicEvents){
                        musicEvent.onCompletion(dataMusics.get(i).getLyric());
                    }
                    mPlayer.setDataSource(dataMusics.get(i).getUrl());
                    //准备播放歌曲
                    mPlayer.prepare();
                    //开始播放歌曲
                    mPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void clearAll() {
        dataMusics.clear();
        i=-1;
        for (MusicEvent musicEvent: musicEvents){
            musicEvent.onNull();
        }
        mPlayer.reset();
    }

    public int getPn() {
        return i;
    }
    public void setPn(int i) {
        if(this.i==i){
            return;
        }
        if(i<0){
            i=0;
        }
        try {
            mPlayer.reset();
            if(dataMusics.size()<=i){
                i=0;
            }
            this.i = i;
            for (MusicEvent musicEvent: musicEvents){
                musicEvent.onCompletion(dataMusics.get(i).getLyric());
                musicEvent.onCurrent(dataMusics.get(i));
            }
            mPlayer.setDataSource(dataMusics.get(i).getUrl());
            //准备播放歌曲
            mPlayer.prepare();
            //开始播放歌曲
            mPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void remove(int j){
        dataMusics.remove(j);
        if(i==j){
            if((i-1)>=dataMusics.size()-1){
                i=0;
            }
            try {
                mPlayer.reset();
                if(dataMusics.size()==0){
                    return;
                }
                for (MusicEvent musicEvent: musicEvents){
                    musicEvent.onCompletion(dataMusics.get(i).getLyric());
                    musicEvent.onCurrent(dataMusics.get(i));
                }
                mPlayer.setDataSource(dataMusics.get(i).getUrl());
                //准备播放歌曲
                mPlayer.prepare();
                //开始播放歌曲
                mPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(i<j){

        }else {
            i--;
        }
        for (MusicEvent musicEvent: musicEvents){
            musicEvent.onData(dataMusics,i);
        }
    }

    public Context getAppContext() {
        return getAppContext();
    }

    public class ImageFactory implements CarouselImageFactory {
        @Override
        public void onLoadFactory(String url, ImageView view) {
            Glide.with(view).load(Integer.parseInt(url)).into(view);
        }
    }

    public void addDataMusics(DataMusic dataMusic,MusicEvent event){
        try {
            musicEvents.add(event);
            if(dataMusic!=null){
                if(mPlayer.isPlaying()){
                    mPlayer.stop();
                }
                mPlayer.reset();
                dataMusics.add(i+1,dataMusic);
                i++;
                mPlayer.setDataSource(dataMusic.getUrl());
                for (MusicEvent musicEvent: musicEvents){
                    musicEvent.onCurrent(dataMusic);
                }
                //准备播放歌曲
                mPlayer.prepare();
                //开始播放歌曲
                mPlayer.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addDataMusics(List<DataMusic> dataMusics,MusicEvent event){
        try {
            musicEvents.add(event);
            if(dataMusics!=null){
                if(mPlayer.isPlaying()){
                    mPlayer.stop();
                }
                this.dataMusics.addAll(i+1,dataMusics);
                i+=dataMusics.size();
                mPlayer.reset();
                mPlayer.setDataSource(dataMusics.get(0).getUrl());
                for (MusicEvent musicEvent: musicEvents){
                    musicEvent.onCurrent(dataMusics.get(0));
                }
                //准备播放歌曲
                mPlayer.prepare();
                //开始播放歌曲
                mPlayer.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public List<DataMusic> getDataMusics(){
        return dataMusics;
    }
    public DataMusic getDataMusic(){
        return dataMusics.get(i);
    }

    public boolean isPause(){
        return !mPlayer.isPlaying();
    }
    public void pauseStart(){
        try {
            if (mPlayer.isPlaying()){
                mPlayer.pause();
            }else {
                mPlayer.start();
            }
        }catch (IllegalStateException e){
            e.printStackTrace();
        }
        for (MusicEvent musicEvent: musicEvents){
            musicEvent.onStateChange(mPlayer.isPlaying());
        }

    }


    public void setSeekTo(int seek){
        mPlayer.seekTo(seek);
    }

    public long getCurrentPosition(){
        return mPlayer.getCurrentPosition();
    }

    public long getDuration(){
        return mPlayer.getDuration();
    }


    public interface MusicEvent{
        void onCompletion(String lyric);
        void onCurrent(DataMusic dataMusic);
        void onStateChange(Boolean bool);
        void onNull();
        void onData(List<DataMusic> dataMusics, int i);
    }
}

